package net.tngou.iask.action.api;

import net.tngou.iask.action.BaseAction;
import net.tngou.iask.enums.LevelEnum;
import net.tngou.iask.pojo.Advice;
import net.tngou.iask.util.StringUtil;

/**
 * 问卷建议
 * @author tngou@tngou.net
 *
 */
public class AdviceApi extends BaseAction {

	/**
	 * 添加问卷建议
	 */
	public void add() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2&&user.getRole()!=3)
		{
			run_false("没有添加权限");return;
		}
		Advice bean = getAsk(Advice.class);		
		if(isEmpty(bean.getName(),bean.getDescription())||bean.getQuestion()==0)
		{
			run_false("请求的参数不正确！");return;
		}
		bean.setMain(user.getMain());
		long b = bean.save();
		if(b>0){
			saveLog(LevelEnum.system, "问卷建议(id:"+b+"-"+bean.getName()+")");
			run_success(b+"");return;
		}
		else {run_false("添加失败！");return;}
	}
	
	/**
	 * 更新
	 */
	public void update() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2&&user.getRole()!=3)
		{
			run_false("没有编辑权限");return;
		}
		Advice bean = getAsk(Advice.class);
		if(bean.getId()==0)
		{
			run_false("请求的参数不正确！");return;
		}
		Advice advice = new Advice().get(bean.getId());
		if(advice==null||advice.getMain()!=user.getMain())
		{
			run_false("请求的数据不正确！");return;
		}
		if(StringUtil.isNotEmpty(bean.getName()))advice.setName(bean.getName());
		if(StringUtil.isNotEmpty(bean.getDescription()))advice.setDescription(bean.getDescription());
		if(StringUtil.isNotEmpty(request.getParameter("question")))advice.setQuestion(bean.getQuestion());
		if(StringUtil.isNotEmpty(request.getParameter("value")))advice.setValue(bean.getValue());
		if(StringUtil.isNotEmpty(request.getParameter("score")))advice.setScore(bean.getScore());
		long b = advice.update();
		if(b>0){
			saveLog(LevelEnum.system, "编辑问题建议(id:"+advice.getId()+"-"+advice.getName()+")");
			run_success("编辑成功！");return;
		}else{
			run_false("编辑失败");return;
		}
		
	}
	/**
	 * 删除
	 */
	public void delete() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2&&user.getRole()!=3)
		{
			run_false("没有删除权限");return;
		}
		if(ask.getId()==0)
		{
			run_false("请求的参数不正确！");return;
		}
		Advice advice = new Advice().get(ask.getId());
		if(advice==null||advice.getMain()!=user.getMain())
		{
			run_false("请求的数据不正确！");return;
		}
		boolean b = advice.delete();
		if(b){
			saveLog(LevelEnum.system, "编辑问题建议(id:"+advice.getId()+"-"+advice.getName()+")");
			run_success("删除成功！");return;
		}else{
			run_false("删除失败");return;
		}
		
	}
}
